package com.ffapp.demo.chemicals;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisActivity;
import com.ffapp.demo.basis.Constants;
import com.ffapp.demo.bean.BaseListBean;
import com.ffapp.demo.bean.BaseListBeanXF;
import com.ffapp.demo.bean.ChemicalData;
import com.ffapp.demo.bean.ChemicalListBean;
import com.ffapp.demo.util.OnClickUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 警情列表
 */
public class ChemicalListActivity extends BasisActivity {

    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private MyAdapterList mAdapter;
    private ChemicalListBean mListBean;

    @BindView(R.id.edit_search)
    EditText edit_search;
    String searchkey = "";
    boolean isSearch = false;


    @Override
    public void initViews() {
        setContentView(R.layout.base_xrecycler_with_top_and_search);
        setTitle("危化品");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(true);



    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mListBean = new ChemicalListBean();
        setListView();
        getList();
        setTitleLeftButton(null);
//        edit_search.setHint("请输入关键字");
        edit_search.setHint("名称|状态|颜色|危险性");
        edit_search.setOnKeyListener(new View.OnKeyListener() {
            @Override

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(ChemicalListActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if(event.getAction() == KeyEvent.ACTION_UP){
                        search();
                    }
                }
                return false;
            }
        });
    }

    @OnClick(R.id.img_search)
    void search() {
        searchkey = edit_search.getText().toString().trim();
        mListBean.refresh();
        getList();
    }

    @OnClick(R.id.img_cancel)
    void searchCancel() {
        edit_search.setText("");
    }

    @OnClick(R.id.text_cancel)
    void canel() {
        edit_search.setText("");
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
//        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }


    private void getList() {

        MyParams params = new MyParams();
        showProgress();

        params.put(BaseListBeanXF.PAGE_NAME, mListBean.getNextPage());
        params.put(BaseListBeanXF.PAGE_SIZE_NAME, BaseListBean.PAGE_SIZE);
        if(!TextUtils.isEmpty(searchkey)){
            params.put("keyWord",searchkey);
        }

        MyHttpUtils.post(Constants.URL_CHEMICAL_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        ChemicalListBean bean = (ChemicalListBean) result;
                        mListBean.addListBean(bean);
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                        onListViewComplete();
                    }
                },
                0, ChemicalListBean.class);

    }


    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.base_list_item_three, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_value)
            public TextView text_value;
            @BindView(R.id.img_icon)
            public ImageView img_icon;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
                img_icon.setVisibility(View.VISIBLE);
            }

            public void bind(int position) {
                ChemicalData data = mListBean.getList().get(position);
                img_icon.setImageResource(R.drawable.icon_doc);
                text_name.setText(""+data.getCnname());
                text_value.setText(""+data.getStatus()+data.getYs()+data.getWxx());
                list_item.setTag(position);

            }
            @OnClick(R.id.list_item)
            void onItemClick(View view){
                if (!OnClickUtils.isFastClick()) {
                    // 进行点   击事件后的逻辑操作
                    int position = (int) view.getTag();
                    ChemicalData listData = mListBean.getList().get(position);
                    ChemicalInfoActivity.toActivity(mContext,listData);
                }
            }
        }
    }

    public final static int WARNLISTRESULT = 44;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == WARNLISTRESULT){
            mListBean.refresh();
            getList();
        }
    }
}
